package com.ruoyi.utils;


import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * @Title TODO
 * @Description TODO
 * @Author CAI ZHAI
 * @Date 2021/9/17 22:20
 **/
public class POIUtil {
    
    /*public static void readExcel(String fileName) throws Exception {
        
        InputStream is = new FileInputStream(new File(fileName));
        Workbook hssfWorkbook = null;
        if (fileName.endsWith("xlsx")) {
            hssfWorkbook = new XSSFWorkbook(is);// Excel 2007
        } else if (fileName.endsWith("xls")) {
            hssfWorkbook = new HSSFWorkbook(is);// Excel 2003
            
        }
        // HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
        // XSSFWorkbook hssfWorkbook = new XSSFWorkbook(is);
        TbClass tbClass = null;
        List<TbClass> list = new ArrayList<TbClass>();
        // 循环工作表Sheet
        for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
            // HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
            Sheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
            if (hssfSheet == null) {
                continue;
            }
            // 循环行Row
            for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
                Row hssfRow = hssfSheet.getRow(rowNum);
                if (hssfRow != null) {
                    tbClass = new TbClass();
                    Cell cId = hssfRow.getCell(0);
                    Cell cName = hssfRow.getCell(1);
                    // 处理具体的业务数据，把业务数据装到List中
                    tbClass.setCId(Integer.parseInt(getStringValueFromCell(cId)));
                    tbClass.setCName(cName.toString());
                    list.add(tbClass);
                }
            }
        }
        // List中的数据就是在Excel中读取的内容
        for (TbClass tbClass2 : list) {
            // 在这里可以进行业务操作
            System.out.println(tbClass2.getCId());
            System.out.println(tbClass2.getCName());
        }
        
    }
    
    
    public static String getStringValueFromCell(Cell cell) {
        SimpleDateFormat sFormat = new SimpleDateFormat("MM/dd/yyyy");
        DecimalFormat decimalFormat = new DecimalFormat("#.#");
        String cellValue = "";
        if(cell == null) {
            return cellValue;
        }
        else if(cell.getCellType() == CellType.STRING) {
            cellValue = cell.getStringCellValue();
        }
        
        else if(cell.getCellType() == CellType.NUMERIC) {
            if(HSSFDateUtil.isCellDateFormatted(cell)) {
                double d = cell.getNumericCellValue();
                Date date = HSSFDateUtil.getJavaDate(d);
                cellValue = sFormat.format(date);
            }
            else {
                cellValue = decimalFormat.format((cell.getNumericCellValue()));
            }
        }
        else if(cell.getCellType() == CellType.BLANK) {
            cellValue = "";
        }
        else if(cell.getCellType() == CellType.BOOLEAN) {
            cellValue = String.valueOf(cell.getBooleanCellValue());
        }
        else if(cell.getCellType() == CellType.ERROR) {
            cellValue = "";
        }
        else if(cell.getCellType() == CellType.FORMULA) {
            cellValue = cell.getCellFormula().toString();
        }
        return cellValue;
    }*/
}